#Boa:Dialog:comisiones

import wx
import wx.grid
import consultasBD
import utiles

def create(parent):
    return comisiones(parent)

[wxID_COMISIONES, wxID_COMISIONESBUTTON1, wxID_COMISIONESCOMBOBOX1, 
 wxID_COMISIONESGRID1, wxID_COMISIONESINMES, wxID_COMISIONESMESES, 
 wxID_COMISIONESSTATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(7)]

class comisiones(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_COMISIONES, name='comisiones',
              parent=prnt, pos=wx.Point(390, 60), size=wx.Size(549, 456),
              style=wx.DEFAULT_DIALOG_STYLE, title='Comisiones')
        self.SetClientSize(wx.Size(541, 429))

        self.grid1 = wx.grid.Grid(id=wxID_COMISIONESGRID1, name='grid1',
              parent=self, pos=wx.Point(16, 40), size=wx.Size(512, 384),
              style=0)
        self.grid1.Bind(wx.EVT_SET_FOCUS, self.OnFocus)

        self.staticText1 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT1,
              label='MES', name='staticText1', parent=self, pos=wx.Point(80, 8),
              size=wx.Size(20, 13), style=0)

        self.inmes = wx.TextCtrl(id=wxID_COMISIONESINMES, name='inmes',
              parent=self, pos=wx.Point(104, 8), size=wx.Size(32, 21), style=0,
              value='')
        self.inmes.SetEditable(False)

        self.meses = wx.ComboBox(choices=['Enero', 'Febrero', 'Marzo', 'Abril',
              'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre',
              'Noviembre', 'Diciembre'], id=wxID_COMISIONESMESES, name='meses',
              parent=self, pos=wx.Point(136, 8), size=wx.Size(130, 21), style=0,
              value='Meses...')
        self.meses.Bind(wx.EVT_COMBOBOX, self.OnMesesCombobox,
              id=wxID_COMISIONESMESES)

        self.comboBox1 = wx.ComboBox(choices=[], id=wxID_COMISIONESCOMBOBOX1,
              name='comboBox1', parent=self, pos=wx.Point(16, 8),
              size=wx.Size(56, 21), style=0, value='')
        self.comboBox1.SetLabel('')
        self.comboBox1.Bind(wx.EVT_COMBOBOX, self.OnCambiaAnio,
              id=wxID_COMISIONESCOMBOBOX1)

        self.button1 = wx.Button(id=wxID_COMISIONESBUTTON1, label='Salir',
              name='button1', parent=self, pos=wx.Point(448, 8),
              size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnSalir,
              id=wxID_COMISIONESBUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.llenarCombo()
        self.meses.SetEditable(False)
        self.meses.SetFocus()
        self.cargaGrilla()

    def llenarCombo(self):
        anio = int(utiles.obtenerFecha("Y-m-d")[:4])
        for i in xrange(2008,anio+1):
            self.comboBox1.Append(str(i))
        self.comboBox1.SetSelection(len(self.comboBox1.GetItems())-1)
    
    def autosizegrilla(self):
        for i in xrange(self.grid1.GetNumberCols()):
            self.grid1.AutoSizeColumn(i,True)
        self.grid1.Refresh()

    def cargaGrilla(self):
        self.grid1.CreateGrid(20,6)
        columnas2 =['Vendedora','Monto Vendido','En Pesos','Tarjetas\ny otros','Ctas.Ctes','Comision']
        for i, col in enumerate(columnas2): 
            self.grid1.SetColLabelValue(i, col)
            
        self.grid1.AutoSizeRow(0,True)
        self.grid1.EnableEditing(False)
        self.grid1.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        self.autosizegrilla()
    
    def OnMesesCombobox(self, event):
        
        self.limpiaGrilla()
        mes = self.meses.GetValue()
        if mes=="Enero":
            mes="01"
        if mes=="Febrero":
            mes="02"
        if mes=="Marzo":
            mes="03"
        if mes=="Abril":
            mes="04"
        if mes=="Mayo":
            mes="05"
        if mes=="Junio":
            mes="06"
        if mes=="Julio":
            mes="07"
        if mes=="Agosto":
            mes="08"
        if mes=="Setiembre":
            mes="09"
        if mes=="Octubre":
            mes="10"
        if mes=="Noviembre":
            mes="11"
        if mes=="Diciembre":
            mes="12"
        
        self.inmes.ChangeValue(str(mes))
        anio = self.comboBox1.GetLabel()
        
        diainicio,diafin = utiles.getDaysExtreme2(mes)
        diainicio = utiles.convertirFecha(diainicio)[:2]
        diafin  = utiles.convertirFecha(diafin)[:2]

        fechaInicio= '%s-%s-%s' % (anio,mes,diainicio)
        fechaFinal= '%s-%s-%s' % (anio,mes,diafin)

        r01 = "SELECT b.nombre AS 'Nombre', SUM( totalfactura ) AS 'Total', \n"
        r02 = "       (SELECT sum( monto ) \n"
        r03 = "        FROM ventastienepagos c \n"
        r04 = "        WHERE idformapagos = 'P' AND c.vendedora = b.idvendedora \n"
        r05 = "              AND c.vendedora=a.usuario \n"
        r06 = "              AND (fecha>='%s' AND fecha<='%s')) AS Pesos,\n" % (fechaInicio,fechaFinal)
        r07 = "       (SELECT sum( monto) FROM ventastienepagos c \n"
        r08 = "        WHERE idformapagos <>'P' AND c.vendedora = b.idvendedora \n"
        r09 = "              AND c.vendedora=a.usuario \n"
        r10 = "              AND (fecha>='%s' AND fecha<='%s')) AS Tarjetas \n" % (fechaInicio,fechaFinal)
        r11 = "FROM ventas a, vendedoras b \n"
        r12 = "WHERE a.usuario = b.idvendedora \n"
        r13 = "      AND (a.fecha>='%s' AND a.fecha<='%s') \n" % (fechaInicio,fechaFinal)  
        r14 = "      AND idformaticket!=999 \n"
        r15 = "GROUP BY Nombre ORDER BY Total DESC \n"    
        SQL = r01+r02+r03+r04+r05+r06+r07+r08+r09+r10+r11+r12+r13+r14+r15

        resultados = consultasBD.ordenCrudaDict(SQL)
        i =0 
        for r in resultados:
            total = float("0.0")
            pesos = float("0.0")
            tarjetas = float("0.0")
            ctacte = float("0.0")
            if r["Nombre"] != None:
                self.grid1.SetCellValue(i, 0, str(r["Nombre"]))
            if r["Total"] != None:
                total = round(r["Total"],2)
                self.grid1.SetCellValue(i, 1, str(round(r["Total"],2)))
                self.grid1.SetCellAlignment(i, 1, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            if r["Pesos"] != None :
                pesos = round(r["Pesos"],2)
                self.grid1.SetCellValue(i, 2, str(round(r["Pesos"],2))) 
                self.grid1.SetCellAlignment(i, 2, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            if r["Tarjetas"] != None :
                tarjetas = round(r["Tarjetas"],2)
                self.grid1.SetCellValue(i, 3, str(round(r["Tarjetas"],2))) 
                self.grid1.SetCellAlignment(i, 3, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            if r["Total"] != None  :
                comision = (1 * round(r["Total"],2)) / 100
                ctacte = total - pesos - tarjetas
                self.grid1.SetCellValue(i, 4, str(round(ctacte,2))) 
                self.grid1.SetCellAlignment(i, 4, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
                self.grid1.SetCellValue(i, 5, str(round(comision,2)))
                self.grid1.SetCellAlignment(i, 5, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            
            i = i + 1
        
        #self.grid1.SetCellValue(i, 4,   ) #
        total1 = float("0.0")
        total2 = float("0.0")
        total3 = float("0.0")
        total = float("0.0")
        for r in xrange(i):
            if self.grid1.GetCellValue(r,1) != "":
                total1 = total1 + float(self.grid1.GetCellValue(r,1))
            if self.grid1.GetCellValue(r,2) != "":
                total2 = total2 + float(self.grid1.GetCellValue(r,2))
            if self.grid1.GetCellValue(r,3) != "":
                total3 = total3 + float(self.grid1.GetCellValue(r,3))
            if self.grid1.GetCellValue(r,4) != "":
                total = total + float(self.grid1.GetCellValue(r,4))
        
        self.grid1.SetCellValue(i ,0,"TOTALES")
        self.grid1.SetCellValue(i ,1,str(total1))
        self.grid1.SetCellAlignment(i, 1, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellValue(i ,2,str(total2))
        self.grid1.SetCellAlignment(i, 2, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellValue(i ,3,str(total3))
        self.grid1.SetCellAlignment(i, 3, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellValue(i ,4,str(total))
        self.grid1.SetCellAlignment(i, 4, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.autosizegrilla()
        
    def limpiaGrilla(self):
        self.grid1.ClearGrid()
        self.grid1.Refresh()

    def OnSalir(self, event):
        self.Close()

    def OnFocus(self, event):
        self.meses.SetFocus()

    def OnCambiaAnio(self, event):
        if self.meses.GetLabel()!="Meses...":
            self.OnMesesCombobox(None)
